# libc/f8l Makefile

VPATH  = @srcdir@
srcdir = @srcdir@
top_builddir = @top_builddir@

LIB_TYPE     = @LIB_TYPE@

SCC = $(top_builddir)/bin/sdcc -mf8l --max-allocs-per-node 25000
SAS = $(top_builddir)/bin/sdasf8

# override PORTDIR defined by super (parent) makefile
override PORTDIR = ../build/f8l

include $(srcdir)/../incl.mk

F8_FLOAT = $(COMMON_FLOAT)

F8_INT = $(COMMON_INT) \
  _mulschar.c \
  _muluchar.c \
  _divschar.c \
  _modschar.c \
  _divuchar.c \
  _moduchar.c \
  _modschar.c

F8_LONG = $(COMMON_LONG) \
  _divulong.c \
  _mullong.c

F8_LONGLONG = $(COMMON_LONGLONG) \
  _mullonglong.c \
  _divslonglong.c \
  _divulonglong.c \
  _modslonglong.c \
  _modulonglong.c

F8_SDCC = $(COMMON_SDCC) \
  __itoa.c \
  __ltoa.c \
  _startup.c \
  _strcmp.c \
  _strcpy.c \
  strlen.c \
  strnlen.c \
  memcpy.c \
  __memcpy.c \
  _memmove.c \
  div.c \
  ldiv.c \
  lldiv.c \
  sprintf.c \
  vprintf.c \
  atomic_flag_clear.c

F8SOURCES = $(addprefix ../,$(F8_FLOAT) $(F8_INT) $(F8_LONG) $(F8_LONGLONG) $(F8_SDCC))
F8OBJECTS = $(patsubst %.c,%.rel,$(F8_FLOAT) $(F8_INT) $(F8_LONG) $(F8_LONGLONG) $(F8_SDCC))

OBJ = setjmp.rel _mulint.rel _divsint.rel _divuint.rel _modsint.rel _moduint.rel heap.rel atomic_flag_test_and_set.rel

LIB = f8l.lib
CC = $(SCC)
AS = $(SAS)
ASFLAGS = -plosgff

CFLAGS = -I$(srcdir)/../../include -I. --std-c2x

all: $(PORTDIR)/$(LIB)

$(PORTDIR)/$(LIB): $(OBJ) $(F8OBJECTS) Makefile
ifeq ($(LIB_TYPE), SDCCLIB)
	rm -f $@; \
	$(top_builddir)/bin/sdcclib -a $@ $(OBJ) $(F8OBJECTS)
else
  ifeq ($(LIB_TYPE), AR)
	$(top_builddir)/bin/sdar -rcSD $@ $(OBJ) $(F8OBJECTS)
  else
    ifeq ($(LIB_TYPE), RANLIB)
	$(top_builddir)/bin/sdar -rcD $@ $(OBJ) $(F8OBJECTS)
    else
	rm -f $@
	for i in $(basename $(OBJ) $(F8OBJECTS)); do echo $$i >>$@; done
	cp $(OBJ) $(F8OBJECTS) $(PORTDIR)
    endif
  endif
endif

%.rel: %.c
	$(CC) $(CFLAGS) -c $<

%.rel: %.s
	@# TODO: sdas should place it\'s output in the current dir
	test $(srcdir) = . || cp $< .
	-$(AS) $(ASFLAGS) $(notdir $<)
	test $(srcdir) = . || rm $(notdir $<)

%.rel: ../%.c
	$(CC) $(CFLAGS) -c $<

clean:
	rm -f *.rel *.sym *.lst *~ $(CLEANSPEC) *.dump* *.asm *.lib

distclean: clean
	rm -f Makefile

